﻿@model MvcCmsOptIn.Models.OptInModel 
@{
    string recipients = ViewBag.Recipients.ToString();
}           
@section HtmlHead{
    <link href="@Url.Content("/areas/mvccmsoptin/content/start/jquery-ui-1.8.11.custom.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("/RazorThemes/" + Model._Portal.SkinName + "/css/optin.css")" rel="stylesheet" type="text/css" />
}
<div class="contentpage-outerdiv">
    <div id="container">
        <div class="breadcrumb">
            <span><a href="/optin-manager">Main Menu</a></span><span>» </span><span><a href="/optin-manager/newsletters">NewsLetters</a></span><span>» </span><span>Send</span>
        </div>
        <div id="content">
            <h2>
                Send Newsletter</h2>
            <div id="progressbar"></div>
            <div style="margin-top:5px;" align="right">
                <input type="submit" id="btnStart" onclick="SendNewsLetter(0);" value="Send" name="btnStart" />
                &nbsp;
                <input type="submit" id="btnStop" onclick="Abort();" value="Abort" name="btnStop" disabled="" />
            </div>
            <br />
            <div class="newsletter_preview_container">
                @Html.Raw(Model._NewsLetter.HTML)            
                @Html.Partial("ControlsOptIn/Footer", Model)
            </div>
        </div>
    </div>
</div>
@Html.Hidden("NewsLetterID", @Model._NewsLetter.NewsLetterID)
@Html.Hidden("Recipients", recipients)
@section ScriptContent{
    <script src="@Url.Content("/areas/mvccmsoptin/scripts/jquery-ui-1.8.11.custom.min.js")" type="text/javascript"></script>
    <script id="Ready" type="text/javascript" charset="utf-8">
        var recipients = $("#Recipients").val().split(',');
        var abort = false;
        $(document).ready(function () {
            $(function () {
                $("#progressbar").progressbar({
                    value: 0
                });
            });
        });
    </script>
    <script id="SendNewsLetter" type="text/javascript" charset="utf-8">
        function Abort() {
            abort = true;
        }
        function SendNewsLetter(i) {
            if (abort) { alert("Stopping"); return false; }
            if (i > recipients.length - 1) {
                $("#progressbar").progressbar("option", "value", 100);
                return false;
            }
            var subscriberId = recipients[i];
            $("#btnStop").disabled = false;
            $.post("/optinajax/sendnewsletter/",
		    {
		        SubscriberID: subscriberId,
		        NewsLetterID: $('#NewsLetterID').val()
		    },
            function (data) {
                if (data.error) {
                    if (confirm(data.error)) {
                        i += 1;
                        SendNewsLetter(i);
                    }
                    else {
                        return false;
                    }
                }
                else {
                    var percentDone = (100 / recipients.length) * i+1;
                    $("#progressbar").progressbar("option", "value", percentDone);
                    i += 1;
                    SendNewsLetter(i);
                }
            }, "json");            
        }        
    </script>    
}
@section SideBar{
}
